/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package an_sim;

import java.awt.BorderLayout;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author Karcsi
 */
public class treeView extends View {

    private JTree tree;

    public treeView(FullNetwork fn) {
        super(fn);
    }

    public JTree getTree() {
        return tree;
    }

    private VirtualNetwork topLevel(VirtualNetwork top) {
        if (top.getGatewayIdUp() != 0) {
            return topLevel(fn.getVNByID(top.getGatewayIdUp()));
        }
        return top;
    }

    private void createNodes(DefaultMutableTreeNode top, VirtualNetwork vn) {
        Iterator<Integer> lowerMembers = vn.getGatewayIdDown().iterator();
        while (lowerMembers.hasNext()) {
            int lowerMember = lowerMembers.next();
            DefaultMutableTreeNode tempTreeNode = new DefaultMutableTreeNode(lowerMember);
            top.add(tempTreeNode);
            if (lowerMember != 0) {
                createNodes(tempTreeNode, fn.getVNByID(lowerMember));
            }
        }

    }

   public JTree buildTree() {

        VirtualNetwork vnTop = topLevel(fn.getVirtualNetworkList().get(0));
        DefaultMutableTreeNode top =
                new DefaultMutableTreeNode(Integer.toString(vnTop.getSuperNode().getID()));
        createNodes(top, vnTop);
        tree = new JTree(top);

        return tree;

    }

    public void createTree() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JScrollPane scrollPane = new JScrollPane(buildTree());
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}
